package com.jnu.myaccount.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jnu.myaccount.R;
import com.jnu.myaccount.data.GridItems;

import java.util.List;

public class ItemAdapter extends BaseAdapter {

        private List<GridItems> GridItemsList;
        private LayoutInflater layoutInflater;
        public int mSelect = 14;   //选中项


    public ItemAdapter(Context context, List<GridItems> provinceBeanList) {
            this.GridItemsList = provinceBeanList;
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return GridItemsList.size();
        }

        @Override
        public Object getItem(int position) {
            return GridItemsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return  position;
        }

    public void changeSelected(int positon){ //刷新方法
        if(positon != mSelect){
            mSelect = positon;
            notifyDataSetChanged();
        }
    }
    public void setSelected(int a){
        this.mSelect=a;
    }
    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.layout_grid_items, parent,false);
                holder = new ViewHolder();
                holder.text = convertView.findViewById(R.id.tv1);
                holder.imageView = convertView.findViewById(R.id.iv1);
                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            GridItems provinceBean = GridItemsList.get(position);
            if (provinceBean != null) {
                holder.text.setText(provinceBean.getName());
                holder.imageView.setImageResource(provinceBean.getPictureID());
            }
           if(mSelect==position){
               convertView.setBackgroundColor(Color.parseColor("#D3D3D3"));  //选中项背景
               holder.text.setTextColor(Color.parseColor("#FF7F50"));
            }else{
                convertView.setBackgroundColor(Color.parseColor("#F5F5F5"));  //其他项背景
                holder.text.setTextColor(Color.parseColor("#000000"));
            }


            return convertView;
        }

        class ViewHolder {
            TextView text;
            ImageView imageView;
        }


}
